package com.sskj.hangqing.http;

import android.text.TextUtils;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okgo.request.PostRequest;
import com.sskj.common.base.App;
import com.sskj.common.base.HttpData;
import com.sskj.common.http.WebSocketObserver;
import com.sskj.common.util.LanguageUtil;
import com.sskj.hangqing.bean.GuideListBean;
import com.sskj.hangqing.bean.PushTradeBean;
import com.sskj.hangqing.bean.Stock;
import com.sskj.hangqing.bean.SummaryBean;
import com.sskj.hangqing.bean.ZixunListBean;
import com.sskj.lib.bean.AppVersionBean;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.bean.WSFiveBean;
import com.sskj.lib.util.APKVersionCodeUtils;
import com.sskj.lib.util.CommonUtil;

import java.util.List;
import java.util.Locale;

import okhttp3.WebSocket;

/**
 * 作者 :吕志豪
 * 简书：https://www.jianshu.com/u/6e525b929aac
 * github：https://github.com/lvzhihao100
 * 描述：
 * 创建时间：2018-10-15 14:08
 */
public class HttpService {


    /**
     * 行情数据
     *
     * @param code 标识符
     * @return
     */
    public GetRequest<HttpData<List<CoinBean>>> getProduct(String code) {
        String qu = null;
        if (code != null) {
            qu = code.replace("_", "/").toLowerCase().split("/")[1];
        }
        return OkGo.<HttpData<List<CoinBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_PRODUCT)
                .params("qu", qu)
                .params("code", CommonUtil.dealReuqestCode(code));
    }

    /**
     * 行情数据
     *
     * @return
     */
    public GetRequest<HttpData<List<CoinBean>>> getProductQu(String qu) {
        return OkGo.<HttpData<List<CoinBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_PRODUCT)
                .params("qu", qu)
                ;
    }

    /**
     * 行情数据
     *
     * @return
     */
    public GetRequest<HttpData<List<CoinBean>>> getSpecialProduct(String qu) {
        return OkGo.<HttpData<List<CoinBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_SPECIAL_PRODUCT)
                .params("qu", qu)
                ;
    }


    /**
     * 获取k线数据
     *
     * @param goodsType 分时数据标示
     * @param code      币种
     * @return
     */
    public GetRequest<HttpData<List<Stock>>> getKData(String goodsType, String code) {
        return OkGo.<HttpData<List<Stock>>>get(HttpConfig.BASE_URL + HttpConfig.GET_K_DATA)
                .params("goodsType", goodsType)
                .params("code", CommonUtil.dealReuqestCode(code));
    }


    /**
     * 获取轮播图
     *
     * @return
     */
    public GetRequest<HttpData<List<String>>> getBanner() {
        return OkGo.<HttpData<List<String>>>get(HttpConfig.BASE_URL + HttpConfig.GET_BANNER)
                .params("type", LanguageUtil.isSimpleChinese() ? "1" : "2")
                ;
    }






    /**
     * 实时成交
     *
     * @param code
     * @return
     */
    public WebSocketObserver pushCoinTrade(String code) {
        return new WebSocketObserver(HttpConfig.WS_BASE_URL) {
            @Override
            public void onOpen(WebSocket webSocket, okhttp3.Response response, String msg) {
                if (TextUtils.isEmpty(msg)) {
                    webSocket.send("{\"sub\":\"trader@" + code + "\"}");
                }else {
                    webSocket.send(msg);
                }
            }
        };
    }






    public PostRequest<HttpData<SummaryBean>> getSummary(String code) {
        return OkGo.<HttpData<SummaryBean>>post(HttpConfig.BASE_URL + HttpConfig.GET_SUMMARY)
                .params("code", CommonUtil.dealReuqestCode(code))
                ;
    }

    public GetRequest<HttpData<List<WSFiveBean>>> getDeepData(String code) {
        return OkGo.<HttpData<List<WSFiveBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_DEEP)
                .params("code", CommonUtil.dealReuqestCode(code))
                .params("type", "pct")
                ;
    }

    public GetRequest<HttpData<PushTradeBean>> getAllTrade(String code) {
        return OkGo.<HttpData<PushTradeBean>>get(HttpConfig.BASE_URL + HttpConfig.GET_ALL_TRADE)
                .params("code", CommonUtil.dealReuqestCode(code))
                .params("num", 15)
                ;
    }

    public GetRequest<HttpData<PageBean<ZixunListBean>>> getZixunList(String page) {
        return OkGo.<HttpData<PageBean<ZixunListBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_ZIXUN_LIST)
                .params("page", page)
                .params("lang", LanguageUtil.isSimpleChinese() ? "zh" : "en")
                .params("s", 20)
                ;
    }

    public PostRequest<HttpData<PageBean<GuideListBean>>> getGuideList(String page) {
        return OkGo.<HttpData<PageBean<GuideListBean>>>post(HttpConfig.BASE_URL + HttpConfig.GET_GUIDE_LIST)
                .params("page", page)
                .params("s", 15)
                ;
    }


    public WebSocketObserver pushCoinFiveDeep(String code) {
        return new WebSocketObserver(HttpConfig.WS_BASE_URL) {
            @Override
            public void onOpen(WebSocket webSocket, okhttp3.Response response, String msg) {
                if (TextUtils.isEmpty(msg)) {
                    webSocket.send("{\"sub\":\"pct@" + code + "\"}");
                }else {
                    webSocket.send(msg);
                }
            }
        };
    }

    public PostRequest<HttpData<AppVersionBean>> getVersion() {
        return OkGo.<HttpData<AppVersionBean>>post(HttpConfig.BASE_URL + HttpConfig.VERSION)
                .params("version", APKVersionCodeUtils.getVerName(App.INSTANCE))
                .params("type", "1");


    }



    public GetRequest<HttpData<PageBean<GuideListBean>>> getGonggaoList(String page) {
        return OkGo.<HttpData<PageBean<GuideListBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_GONGGAO_LIST)
                .params("lang_type", LanguageUtil.isSimpleChinese() ? "1" : "2")
                .params("article_type", "1")
                .params("page", page)
                .params("size", 15)
                ;
    }

    public GetRequest<HttpData<GuideListBean>> getGonggaoInfo(String id) {
        return OkGo.<HttpData<GuideListBean>>get(HttpConfig.BASE_URL + HttpConfig.GET_GONGGAO_INFO)
                .params("id", id)
                ;
    }


    public PostRequest<HttpData<String>> getXieyi(String xieyi) {
        return OkGo.<HttpData<String>>post(HttpConfig.BASE_URL + HttpConfig.XIEYI)
                .params("type", xieyi + (LanguageUtil.getAppLocale(App.INSTANCE) == Locale.ENGLISH ? "1" : ""))
                ;
    }

    public GetRequest<HttpData<String>> getAboutUs() {
        return OkGo.<HttpData<String>>get(HttpConfig.BASE_URL + HttpConfig.ABOUT_US)
                .params("key", "aboutus" + (LanguageUtil.isSimpleChinese() ? "zh" : "en"))
                ;
    }

    public GetRequest<HttpData<String>> getRegister() {
        return OkGo.<HttpData<String>>get(HttpConfig.BASE_URL + HttpConfig.ABOUT_US)
                .params("key", "agreement" + (LanguageUtil.isSimpleChinese() ? "zh" : "en"))
                ;
    }


    public GetRequest<HttpData<PageBean<GuideListBean>>> getNotice() {
        return OkGo.<HttpData<PageBean<GuideListBean>>>get(HttpConfig.BASE_URL + HttpConfig.NOTICE)
                .params("lang_type", LanguageUtil.isSimpleChinese() ? "1" : "2")
                .params("article_type", 1)
                ;
    }

    public GetRequest<HttpData<List<String>>> getZixunBanner() {
        return OkGo.<HttpData<List<String>>>get(HttpConfig.BASE_URL + HttpConfig.ZIXUN_BANNER)

                ;
    }
}